package com.shanzmoo.db.store.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.shanzmoo.db.store.entity.StoreMemberStockEntity;
import com.shanzmoo.db.store.entity.StoreMemberStockIoEntity;
import com.shanzmoo.db.store.entity.vo.StoreMemberStockVO;
import com.shanzmoo.db.store.entity.query.StoreMemberStockQuery;
import com.shanzmoo.db.store.mapper.StoreMemberStockMapper;
import com.shanzmoo.db.store.service.IStoreMemberStockIoService;
import com.shanzmoo.db.store.service.IStoreMemberStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 仓储用户库存实时明细 服务实现类
 *
 * @author Gzhao 2021-03-08
 */
@Service
public class StoreMemberStockServiceImpl extends ServiceImpl<StoreMemberStockMapper, StoreMemberStockEntity>
        implements IStoreMemberStockService {

    @Autowired
    private IStoreMemberStockIoService storeMemberStockIoService;

    @Override
    public List<StoreMemberStockVO> queryPageList(StoreMemberStockQuery query) {
        return baseMapper.queryPageList(query);
    }

    @Override
    public StoreMemberStockEntity getBySkuAndStoreCode(Integer memberId, String sku, String storeCode) {
        Assert.isTrue(StrUtil.isNotEmpty(sku),"未获取到sku信息，库存查询失败");
        Assert.isTrue(StrUtil.isNotEmpty(storeCode),"未获取到仓库编码信息，库存查询失败");

        return this.lambdaQuery()
                .eq(StoreMemberStockEntity::getDeleted, false)
                .eq(StoreMemberStockEntity::getMemberId, memberId)
                .eq(StoreMemberStockEntity::getSku, sku)
                .eq(StoreMemberStockEntity::getStoreCode, storeCode)
                .one();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
    @Override
    public void updateStockByIo(StoreMemberStockIoEntity memberStockIoEntity, Integer versionId) {
        String sku = memberStockIoEntity.getSku();
        String storeCode = memberStockIoEntity.getStoreCode();
        Boolean flow = memberStockIoEntity.getFlow();
        Integer skuNum = memberStockIoEntity.getSkuNum();
        Integer memberId = memberStockIoEntity.getMemberId();

        Assert.isTrue(StrUtil.isNotEmpty(sku),"未获取到sku信息，用户库存更新失败");
        Assert.isTrue(StrUtil.isNotEmpty(storeCode),"未获取到仓库编码信息，用户库存更新失败");
        Assert.isTrue(ObjectUtil.isNotNull(flow),"未获取到库存流向，用户库存更新失败");
        Assert.isTrue(ObjectUtil.isNotNull(skuNum),"未获取到库存更新数量，用户库存更新失败");
        Assert.isTrue(ObjectUtil.isNotNull(memberId),"未获取到用户xinx，用户库存更新失败");

        Integer row = baseMapper.updateStock(memberId, sku, storeCode, flow ? skuNum:-skuNum, versionId);
        Assert.isTrue(1 == row,"数据异常，用户库存更新失败");

        if (!flow) {
            StoreMemberStockEntity stockEntity = this.getBySkuAndStoreCode(memberId, sku, storeCode);
            Assert.isTrue(stockEntity.getSkuNum() >= 0,"sku：" + stockEntity.getSku() + " 库存不足, 扣减失败");
        }

        // 保存库存流水
        boolean ioSave = storeMemberStockIoService.save(memberStockIoEntity);
        Assert.isTrue(ioSave,"库存写入失败，库存更新失败");
    }
}
